﻿layui.config({
    version: true,
    base: '/Plugin/'
}).extend({ //设定模块别名
    md5: 'md5', //如果 mymod.js 是在根目录，也可以不用设定别名
    common: 'common' //相对于上述 base 目录的子目录
}).use(['element', 'form', 'flow', 'table', 'common', 'util'], function () {
    var $ = layui.$,
        element = layui.element,
        util = layui.util,
        flow = layui.flow,
        table = layui.table,
        common = layui.common,
        jhy = layui.data("jhy"),
        tablelist = null,
        tablehistory = null;


    if (parent.layui.$(".layui-layout-body").html() == undefined) {
        //判断是否有本地存储的用户信息
        if (jhy.user_id != undefined && jhy.user_account != undefined && jhy.user_password != undefined && jhy.user_name != undefined && jhy.user_role != undefined) {
            //ajax 验证账号和密码
            common.userLogin(jhy.user_account, jhy.user_password);
        } else {
            //没有这些信息则清空表。
            layui.data("jhy", null);
            layui.data("url", {
                key: 'url',
                value: window.location.href
            })
            window.location.href = "/admin/login.html";
        }
    }

    var tableload = {
        tablelist: function () {
            tablelist = table.render({
                elem: '#tablelist',
                url: '/Ashx/baby.ashx', //数据接口
                method: 'post',
                where: { functionname: 'jhy_baby_notepad_query', note_table: "0" },
                cols: [[ //表头 
                    { type: "radio" },
                    { field: 'note_content', title: '记录内容', minWidth: 100 },
                    {
                        field: 'note_datetime', title: '创建时间', width: 105, align: "center",
                        templet: function (d) {
                            return util.timeAgo(d.note_datetime, true);
                        }, fixed: "right"
                    }
                ]]
            });
        }, tablehistory: function () {
            tablehistory = table.render({
                elem: '#tablehistory',
                url: '/Ashx/baby.ashx', //数据接口
                method: 'post',
                where: { functionname: 'jhy_baby_notepad_query', note_table: "" },
                cols: [[ //表头 
                    { type: "radio" },
                    { field: 'note_content', title: '记录内容', templet:"<div><s>{{d.note_content}}</s></div>", minWidth: 100 },
                    {
                        field: 'note_datetime', title: '执行时间', width: 105, align: "center",
                        templet: function (d) {
                            return util.timeAgo(d.note_datetime, true);
                        }, fixed: "right"
                    }
                ]]
            });
        }
    }

    tableload.tablelist();
    tableload.tablehistory();


    //添加按钮
    util.fixbar({
        bar1: "&#xe654;",
        click: function (type) {
            //添加按钮
            if (type === 'bar1') {
                layer.prompt({
                    formType: 0,
                    title: '输入记录信息'
                }, function (value, index, elem) {
                    $.ajax({
                        type: "post",
                        url: "/Ashx/baby.ashx",
                        data: { functionname: "jhy_baby_notepad_add", note_content: value, note_userid: jhy.user_id },
                        success: function (msg) {
                            tablelist.reload();
                            layer.close(index);
                        }
                    });

                });
            }
        }
    });



    //修改删除事件
    table.on('rowDouble(tablelist)', function (obj) {
        var data = obj.data;
        layer.open({
            type: 1,
            title: "操作",
            btn: ["删除", "修改", "取消"],
            skin: "jhy-layer-css",
            content: "<div style=\"padding:15px;\"><input type=\"text\" name=\"title\" autocomplete=\"off\" class=\"layui-input\"></div>",
            success: function (layero, index) {
                layero.find("input").val(data.note_content).focus();
            }, yes: function (index, layero) {
                layer.confirm('确认删除<span style=\'color:red\'>【' + data.note_content + '】</span>？', { icon: 3, title: '提示' }, function (index2) {
                    $.ajax({
                        type: "post",
                        url: "/Ashx/baby.ashx",
                        data: { functionname: "jhy_baby_notepad_delete", note_id: data.note_id },
                        success: function (msg) {
                            if (msg == "true") {
                                obj.del();
                            } else {
                                tablelist.reload();
                            }
                            layer.close(index2);
                            layer.close(index);
                        }
                    });
                });

            }, btn2: function (index, layero) {
                var note_content = $.trim(layero.find("input").val());
                if (note_content == "") {
                    layer.msg("不能空着");
                    return false;
                }
                $.ajax({
                    type: "post",
                    url: "/Ashx/baby.ashx",
                    data: { functionname: "jhy_baby_notepad_edit", note_id: data.note_id, note_content: note_content },
                    success: function (msg) {
                        if (msg == "true") {
                            obj.update({
                                note_content: note_content
                            });
                        } else {
                            tablelist.reload();
                        }
                        layer.close(index);
                    }
                });
                return false;
            }, btn3: function (index, layero) {
                layer.close(index);
            }
        });
    });


    //任务完成操作
    table.on('radio(tablelist)', function (obj) {
        var data = obj.data,
            note_id = data.note_id,
            note_state = data.note_state;
        if (obj.checked == true) {
            note_state = "0" ? note_state = "1" : note_state = "0";
            $.ajax({
                type: "post",
                url: "/Ashx/baby.ashx",
                data: { functionname: "jhy_baby_notepad_done", note_id: note_id, note_state: note_state },
                success: function (msg) {
                    if (msg == "true") {
                        obj.del();
                        tablehistory.reload();
                    } else {
                        tablelist.reload();
                        tablehistory.reload();
                    }

                }
            });
        }
    });
    //取消任务操作
    table.on('radio(tablehistory)', function (obj) {
        var data = obj.data,
            note_id = data.note_id,
            note_state = data.note_state;
        if (obj.checked == true) {
            note_state = "0" ? note_state = "0" : note_state = "1";
            $.ajax({
                type: "post",
                url: "/Ashx/baby.ashx",
                data: { functionname: "jhy_baby_notepad_done", note_id: note_id, note_state: note_state },
                success: function (msg) {
                    if (msg == "true") {
                        obj.del();
                        tablelist.reload();
                    } else {
                        tablelist.reload();
                        tablehistory.reload();
                    }

                }
            });
        }
    });

});


